﻿using Anke.SHManage.BLL;
using Anke.SHManage.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Anke.SHManage.Web.Areas.DD.Controllers
{
    public class AmbulanceStateController : Controller
    {
        private static object m_SyncRoot = new Object();//互斥对象
        //
        // GET: /DD/AmbulanceState/

        public ActionResult AmbulanceStateList()
        {
            return View();
        }

        //查询
        public ActionResult DataLoad()
        {
            string actualNumber = "";
            //string AmbCode = "";
            string driver = "";
            string doctor = "";
            string nurse = "";
            string stretcher = "";
            string Rescue = "";
            string sortName = "";
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求的页码
            int pageIndex = int.Parse(Request.Form["page"]);
            string AmbCode = Request.Form["AmbCode"];
            if (Request.Form["sort"] == "workState")
            {
                sortName = "工作状态";
            }
            else
            {
                sortName = Request.Form["sort"];
            }           
            string sortOrder = Request.Form["order"];
            //获取查询条件
            string center = Request.Form["center"];
            string station = Request.Form["station"];
            if (Request.Form["actualNumber"] == null)
            {
                actualNumber = "";
            }
            else
            { 
                actualNumber = Request.Form["actualNumber"];
            }
            if (Request.Form["driver"] == null)
            {
                driver = "";
            }
            else
            {
                driver = Request.Form["driver"];
            }
            if (Request.Form["doctor"] == null)
            {
                doctor = "";
            }
            else
            {
                doctor = Request.Form["doctor"];
            }
            if (Request.Form["nurse"] == null)
            {
                nurse = "";
            }
            else
            {
                nurse = Request.Form["nurse"];
            }
            if (Request.Form["stretcher"] == null)
            {
                stretcher = "";
            }
            else
            {
                stretcher = Request.Form["stretcher"];
            }
            if (Request.Form["Rescue"] == null)
            {
                Rescue = "";
            }
            else
            {
                Rescue = Request.Form["Rescue"];
            }
            string ambulanceNumber = Request.Form["ambulanceNumber"];
            //string driver = Request.Form["driver"];
            //string doctor = Request.Form["doctor"];
            //string nurse = Request.Form["nurse"];
            //string stretcher = Request.Form["stretcher"];
            string workState = Request.Form["workState"];

            E_StatisticsPermisson em = UserOperateContext.Current.getMaxPerForStatistics();
            string selfWorkCode = UserOperateContext.Current.Session_UsrInfo.WorkCode;
            string selfCenterID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSubCenterID;
            string selfStationID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSationID;
            string eventDispatchCenterID = UserOperateContext.Current.Seesion_Dispatch_SubCenterID; //2020-11-22 zch 登录人所处急救中心
            AmbulanceStateBLL bll = new AmbulanceStateBLL();

            //2023-08-07 plq 查询页添加SQL防注入
            if (ControlHelper.ProcessSqlStr(center) && ControlHelper.ProcessSqlStr(station) && ControlHelper.ProcessSqlStr(actualNumber)
               && ControlHelper.ProcessSqlStr(ambulanceNumber) && ControlHelper.ProcessSqlStr(driver) && ControlHelper.ProcessSqlStr(doctor)
               && ControlHelper.ProcessSqlStr(nurse) && ControlHelper.ProcessSqlStr(stretcher) && ControlHelper.ProcessSqlStr(workState))
            {

                var list = bll.GetAmbulanceStateList(pageSize, pageIndex, sortName, sortOrder, center, station, actualNumber, ambulanceNumber, driver,
                                                     doctor, nurse, stretcher, workState, em, selfWorkCode, selfCenterID, selfStationID, AmbCode, eventDispatchCenterID);

                JsonResult j = this.Json(list, "appliction/json", JsonRequestBehavior.AllowGet);
                return j;
            }
            else
            {
                return Json(new { total = 0, rows = "dangerousInfomation" }, "appliction/json", JsonRequestBehavior.AllowGet);
            }

        }


        // 获取工作状态
        [HttpPost]
        public ActionResult GetWorkState()
        {
            try
            {
                AmbulanceStateBLL bll = new AmbulanceStateBLL();
                var result = new object();
                //增加缓存
                lock (m_SyncRoot)
                {
                    result = CacheHelper.GetCache("GetWorkState");
                    if (result == null)
                    {
                        result = bll.GetWorkStateList();
                        CacheHelper.SetCache("GetWorkState", result);
                    }
                }
                return Json(result);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }
        public ActionResult GetAmbulance(string AmbCode)
        {
            AmbulanceStateBLL bll = new AmbulanceStateBLL();
            string eventDispatchCenterID = UserOperateContext.Current.Seesion_Dispatch_SubCenterID; //2020-11-22 zch 登录人所处急救中心
            var list = bll.GetAmbulance(AmbCode, eventDispatchCenterID);
            return Json(new { PRInfo = list }, "appliction/json", JsonRequestBehavior.AllowGet);

        }
        public ActionResult AmbulanceStateDetial(string AmbCode)
        {
            ViewBag.AmbCode = AmbCode;
            return View();
        }
    }
}
